<script setup>
import { reactive, onMounted, watch } from 'vue';
import { iuserLogout } from '@T2/service/api.ts';
import { useCurrentWatchStore, useIuserInfoStore } from '@T2/store/users.ts';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { deepCopy, findNodeRoot } from '@T2/utils/utils.ts';
import SvgMask from '@T2/components/common/svgMask.vue';
import EnlargeSvg from '~icons/custom/enlarge-style';
import RowsSvg3 from '~icons/custom/rows-3';

const route = useRoute();
const router = useRouter();
const iuserInfoStore = useIuserInfoStore();
const { info } = iuserInfoStore;
const currentWatchStore = useCurrentWatchStore();
const { curMenus } = currentWatchStore;
const projectList = deepCopy(currentWatchStore.getProjectsOfValidMenus);

onMounted(() => {
  if (!info.company) {
    ElMessage.error('登录过期，请重新登录');
    setTimeout(() => {
      location.href = "/";
    }, 500);
    return;
  }
  init();
});
const goToProject = (project) => {
  const path = getToSomeProjectPath(project.id);
  if (path) {
    router.push(path);
  } else {
    ElMessage.error('暂无登入权限，请联系管理员');
  }
}

const Resd = reactive({
  menus: [],
  asideable: false,
  loaded: false,
});
const init = () => {
  const pcMenus = deepCopy(curMenus);
  Resd.asideable = pcMenus.find(item => item.type === 'PC-aside') ? true : false;
  
  Resd.menus = pcMenus.filter(item => item.type === 'PC-enlarge') || [];
  
  const path = router.currentRoute.value.path;
  if (path === '/C/Display') {
    initMenu();
    Resd.loaded = true;
    return;
  }
  
  let menus = Resd.menus.find(items => items.menu__path === path || items.children?.find(item => item.menu__path === path));
  if (menus) {
    let menu = menus.children?.find(item => item.menu__path === path);
    activeMenu(menus, menu || {});
  } else {
    initMenu();
  }
  Resd.loaded = true;
}
const initMenu = () => {
  let menus = Resd.menus[0];
  let menu = menus.children.length ? menus.children[0] : {};
  activeMenu(menus, menu);
  router.push(menu.id ? menu.menu__path : menus.menu__path);
}
const activeMenu = (menus, menu) => {
  Resd.menus.forEach(items => {
    items.isActive = items.id === menus.id;
    items.children?.forEach(item => {
      item.isActive = item.id === menu?.id;
    });
  })
}
watch(() => route.path, (nv, ov) => {
  if (nv) {
    const menus = findNodeRoot(Resd.menus, 'menu__path', nv);
    const menu = menus.children?.find(item => item.menu__path === nv);
    activeMenu(menus, menu || {});
  }
});

const loginout = () => {
  iuserLogout({
    method: "get",
    params: {}
  }).then(res => {
    router.push('/');
  }).catch(error => {
    router.push('/');
  });
}
</script>

<template>
  <el-container>
    <el-header class="flex--center">
      <el-popover
        v-if="projectList.length"
        popper-style="min-width: auto; padding: 5px 0; transform: translateY(-7px)"
        placement="bottom-end"
        :width="200"
        trigger="click"
      >
        <template #reference>
          <el-icon class="backCo-icon pointer" size="25"><RowsSvg3 /></el-icon>
        </template>
        <div class="p-list flex-column">
          <button v-for="item in projectList" @click="goToProject(item)">{{ item.name }}</button>
        </div>
      </el-popover>
      
      <div class="company-name">{{ info.company_name }}</div>
      <el-icon class="enlarge-tab pointer" @click="router.push('/C/Manage')" v-if="Resd.asideable"> <EnlargeSvg /> </el-icon>
      
      <div class="menu-box">
        <el-menu
          class="el-menu-demo flex-center-center"
          mode="horizontal"
          :ellipsis="false"
          :unique-opened="true"
          :default-active="route.path"
          :router="true"
          v-show="Resd.menus.length > 1"
        >
          <template v-for="(items, indexs) in Resd.menus">
            
            <el-sub-menu popper-class="asideLinePopperClass2" v-if="items.children.length" :index="'' + indexs">
              <template #title>
                <svg-mask
                  :iconFile="items.icon__file__file"
                  :iconColor="items.isActive ? items.icon__color || '#ffffff' : '#5CCCC5'"
                />
                <span>{{ items.alias }}</span>
              </template>
              <el-menu-item :index="item.menu__path" v-for="(item, index) in items.children" v-if="items.children">
                <template #title>{{ item.alias }}</template>
              </el-menu-item>
            </el-sub-menu>
            
            <el-menu-item :index="items.menu__path" v-else>
              <svg-mask
                :iconFile="items.icon__file__file"
                :iconColor="items.isActive ? items.icon__color || '#ffffff' : '#5CCCC5'"
              />
              <template #title>{{ items.alias }}</template>
            </el-menu-item>
            
          </template>
        </el-menu>
      </div>
      <div class="header-right flex-end-center">
        <div class="avatar circle-bg pointer" @click="Resd.avatarPop = true">
          <span>{{ info.truename?.substr(-2, 2) }}</span>
          <template v-if="Resd.avatarPop">
            <div class="layout" @click.stop="Resd.avatarPop = false"></div>
            <div class="avatar-pop flex-column">
              <div class="pop-item detail flex-column flex-center">
                <div class="avatar2 circle-bg">{{ info.truename?.substr(-2, 2) }}</div>
                <span>{{ info.truename }}</span>
                <span>{{ info.contract }}</span>
              </div>
              <div class="pop-item" @click.stop="router.push('/login?reset=true')">修改密码</div>
              <div class="pop-item" @click.stop="loginout()">退出登录</div>
            </div>
          </template>
        </div>
      </div>
    </el-header>
    <el-main>
      <router-view v-if="Resd.loaded" />
    </el-main>
  </el-container>
</template>


<style lang="scss" scoped>
.p-list {
  width: 200px;
  button {
    border: none;
    border-radius: 0;
    background-color: transparent;
    height: 36px;
    line-height: 36px;
    text-align: left;
    white-space: nowrap;
    padding: 0 20px;
    font-weight: 400;
    font-size: 14px;
    color: #303133;
    cursor: pointer;
    &:hover {
      color: #409EFF;
      background-color: #ECF5FF;
    }
  }
}
.backCo-icon {
  width: 50px;
  text-align: center;
}
.company-name {
  white-space: nowrap;
}
.enlarge-tab {
  width: 21px;
  height: 17px;
  margin-left: 23px;
}
.header-right {
  width: 300px;
  height: 100%;
  margin-left: auto;
  .circle-bg {
    width: 37px;
    height: 37px;
    line-height: 38px;
    font-size: 13px;
    border-radius: 50%;
    color: #FFFFFF;
    text-align: center;
  }
  .avatar {
    margin-right: 23px;
    background: linear-gradient(180deg,rgba(23,178,178,.4) 0%,rgba(23,178,178,.88) 100%);
    position: relative;
  }
  .layout {
    position: fixed;
    left: 0;
    top: 0;
    width: 100vw;
    height: 100vh;
    z-index: 999;
  }
  .avatar-pop {
    width: fit-content;
    min-width: 160px;
    background-color: rgba(0,28,32,.96);
    position: absolute;
    right: 50%;
    top: 100%;
    box-shadow: 0 0 2px 2px #00394080;
    border: 1px solid rgba(23,178,178,.6);
    border-radius: 4px;
    overflow: hidden;
    z-index: 1000;
    .pop-item {
      padding: 5px 10px;
      width: 100%;
      height: 45px;
      line-height: 35px;
      border-bottom: 1px solid rgba(23,178,178,.6);
      color: #ffffff;
      font-size: 14px;
      white-space: nowrap;
      text-align: left;
      &:hover {
        background-color: #003940;
        color: #d3f2ee;
      }
    }
    .detail {
      padding-left: 57px;
      color: #d3f2ee;
      position: relative;
      font-size: 12px;
      font-family: PingFangSC,PingFang SC;
      font-weight: 600;
      height: 60px;
      line-height: 20px;
      .avatar2 {
        position: absolute;
        left: 10px;
        top: 50%;
        transform: translateY(-50%);
        background: linear-gradient(180deg,rgba(23,178,178,.4) 0%,rgba(23,178,178,.88) 100%);
      }
    }
  }
}
.menu-box {
  flex: 1;
  display: block;
}
</style>
<style scoped>
.el-container {
  background: url('@T2/assets/enlarge-bg.png') no-repeat center;
  background-size: 100% 100%;
}
.el-header {
  height: 60px;
  line-height: 60px;
  background: url('@T2/assets/header-bg.png') no-repeat center;
  background-size: 100% 100%;
  font-size: 21px;
  padding: 0;
  color: #fff;
}
.el-main {
  padding: 0;
  background-color: transparent;
  position: relative;
  overflow: hidden;
}
:deep(.el-menu.el-menu--horizontal) {
  background-color: transparent;
  border: none;
}

:deep(.el-menu >.el-menu-item),
:deep(.el-menu >.el-sub-menu) {
  height: 35px;
  line-height: 35px;
  width: 180px;
  background: url('@T2/assets/common/tab-no.png') no-repeat center;
  background-size: 100% 100%;
  margin-right: 14px;
}
:deep(.el-menu >.el-menu-item)  {
  font-family: YouSheBiaoTiHei;
  font-size: 24px;
  color: #5CCCC5;
  text-shadow: 0px 4px 8px #003940;
  display: flex;
  justify-content: flex-start;
}
:deep(.el-menu >.el-menu-item.is-active)  {
  color: #D3F2EE !important;
  background: url('@T2/assets/common/tab-selected.png') no-repeat center;
  background-size: 100% 100%;
  border: none;
}
:deep(.el-menu >.el-menu-item:hover)  {
  opacity: 0.85;
}
:deep(.el-menu .el-sub-menu__title)  {
  font-family: YouSheBiaoTiHei;
  font-size: 24px;
  color: #5CCCC5;
  text-shadow: 0px 4px 8px #003940;
  padding-right: 20px;
}
:deep(.el-menu .el-sub-menu__title:hover)  {
  background-color: transparent;
}
</style>